ActiveAdmin.register Order do
  menu label: '课程', parent: '订单'

  actions :index, :show
  filter :member_mobile, as: :string, label: '手机号'
  filter :store_name, as: :string, label: '门店'
  filter :store_city, as: :string, label: '城市'
  filter :order_no
  filter :total
  filter :created_at

  index do
    column :order_no
    column :member_name
    column :member_mobile
    column :course_name
    column :total
    column :coupon
    column :promotion
    column :pay_amount
    column :pay_type
    column :created_at
    column :paid_at
    column :status_info
  end
  show do
    attributes_table do
      row :order_no
      row :member_name
      row :member_mobile
      row :course_name
      row :total
      row :coupon
      row :promotion
      row :promotion_info
      row :pay_amount
      row :pay_type
      row :created_at
      row :paid_at
      row :status_info
    end
  end

  controller do
    def scoped_collection
      Order.joins(:member, :appointment).left_joins(:running_account).select(
          :id,
          :order_no,
          'members.name as member_name',
          'members.mobile as member_mobile',
          "CASE
             WHEN appointments.course_en_name=appointments.course_cn_name then appointments.course_cn_name
             ELSE CONCAT(appointments.course_en_name, '|', appointments.course_cn_name)
           END as course_name",
          "CASE WHEN COALESCE(transactions.price,0) > orders.total THEN COALESCE(transactions.price,0) ELSE orders.total END as total",
          :coupon,
          :promotion,
          :promotion_info,
          :experience_id,
          :experience_info,
          'COALESCE(transactions.price,0) as pay_amount',
          :pay_type,
          "CASE orders.status
             WHEN -4 THEN '付款隐藏'
             WHEN -3 THEN '退款隐藏'
             WHEN -2 THEN '取消隐藏'
             WHEN -1 THEN '取消'
             WHEN 0 THEN '待付款'
             WHEN 1 THEN '已付款'
             WHEN 2 THEN '已退款'
          END as status_info",
          :created_at,
          'transactions.created_at as paid_at'
      )
    end

    def show
      clazz = Object.const_get params[:order_type]
      @order = clazz.send(:find_by, {order_no: params[:id]})
    end

    def index
      if params[:format].eql?("csv") || params[:format].eql?("xls")
        sql = Order.ransack(params[:q]).result.to_sql
        filter = sql.split("WHERE")[-1]
        csv = CSV.generate(headers: true) do |row|
          row << %w{订单编号 会员 会员手机 课程 订单金额 优惠券抵用 活动优惠 活动信息 体验信息 实付金额 付款方式 下单时间 付款时间 订单状态}
          Order.joins(:member, :appointment, :store).left_joins(:running_account).where(filter).pluck(
              Arel.sql(
                  "orders.order_no, members.name, members.mobile,
                CASE appointments.course_en_name WHEN appointments.course_cn_name THEN appointments.course_cn_name ELSE CONCAT(appointments.course_en_name, '|', appointments.course_cn_name) END,
                CASE WHEN COALESCE(transactions.price,0) > orders.total THEN COALESCE(transactions.price,0) ELSE orders.total END,
                orders.coupon, orders.promotion, orders.promotion_info, orders.experience_info,
                COALESCE(transactions.price,0), orders.pay_type, to_char(orders.created_at,'YYYY-MM-DD HH24:MI:SS'), to_char(transactions.created_at,'YYYY-MM-DD HH24:MI:SS'),
                '{\"-4\": \"付款隐藏\",\"-3\": \"退款隐藏\",\"-2\": \"取消隐藏\",\"-1\": \"已取消\",\"-0\": \"待付款\",\"1\": \"已付款\",\"2\": \"已退款\"}'::jsonb -> orders.status::text"
              )
          ).each {|data| row << data}
        end
        send_data(("\uFEFF" + +csv), type: Mime[:csv], disposition: "attachment; filename=订单信息-#{Time.current.to_i}.csv")
      else
        index!
      end
    end
  end
end
